
#include <stdlib.h>

#include "ch.h"
#include "hal.h"

#include "chprintf.h"
#include "myNRF24.h"

static Thread *rt;

/*
 * Remote thread, times are in milliseconds.
 */
static WORKING_AREA(waThreadR, 128);
static msg_t ThreadR(void *arg) {

	int st;
	uint8_t outBuf[40] = {1,2,3};
	
  (void)arg;
	rt = chThdSelf();
  chRegSetThreadName("Remote");
	
	st = 0;
  while (TRUE) {
		chEvtWaitAny((eventmask_t)7);
		NRFSendData(outBuf);
		if (st) {
			palSetPad(GPIOA, GPIOA_LED);
			st = 0;
		} else {
			st = 1;
			palClearPad(GPIOA, GPIOA_LED);
		}
  }
  return 0;
}

void startRemote(void){
  chThdCreateStatic(waThreadR, sizeof(waThreadR), NORMALPRIO, ThreadR, NULL);
}

void remoteSendEvtI(int evt) {
	chEvtSignalI(rt, (eventmask_t)evt);
}

void remoteSendEvt(int evt) {
	chEvtSignal(rt, (eventmask_t)evt);
}
